#!/bin/bash

# 检查当前Node.js版本
CURRENT_VERSION=$(node --version)
echo "当前Node.js版本: $CURRENT_VERSION"

# 检查是否需要升级
if [[ "$CURRENT_VERSION" == v16* || "$CURRENT_VERSION" == v14* || "$CURRENT_VERSION" == v12* ]]; then
    echo "Node.js版本过低，需要升级到v18或更高版本"
    
    # 安装nvm (Node Version Manager)
    echo "正在安装nvm..."
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
    
    # 重新加载bash配置
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
    
    # 安装并使用Node.js 18
    echo "正在安装Node.js 18..."
    nvm install 18
    nvm use 18
    nvm alias default 18
    
    echo "Node.js已升级到版本:"
    node --version
    npm --version
    
    echo "升级完成，请重新启动MCP Server markmap"
else
    echo "Node.js版本满足要求，无需升级"
fi